package moriarty.gwt.client;

import moriarty.gwt.shared.User;
import moriarty.gwt.shared.Utils;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Sotd implements EntryPoint {
	
	//Our RPC service caller.  Use this to access any of the RPC functions we have.
	private final SotdServiceAsync sotdService = GWT.create(SotdService.class);
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		//RPC Call to get the current user's email address, and check if they are a member
    	sotdService.getCurrentUserEmail(new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				//TODO: Log this shit, yo
			}

			@Override
			public void onSuccess(String result) {
				
				//Set our client side local copy of the current user
				CurrentUser.getInstance().setCurrentUserEmail(result);
				
				//Check if the user is a member
				sotdService.isMember(result, new AsyncCallback<Boolean>(){
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onSuccess(Boolean result) {
						CurrentUser.getInstance().setMember(result);	//This is still just a boolean flag
						if(result){
							
							//Get Actual User object from the datastore
							sotdService.getCurrentUser(CurrentUser.getInstance().getCurrentUserEmail(), new AsyncCallback<User>(){
							
								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									
								}
	
								@Override
								public void onSuccess(User result) {								
										CurrentUser.getInstance().setCurrentUser(result);
										initMember();
								}
									
							});							
						}
						else{		
							initNonMember();
						}
					}
				});
			}
    	});
	}
			
			                  		
	private void initMember(){
		// SoTD Banner - gets put on a vertical panel
		final Image banner = new Image( "/images/banner.png");
		final VerticalPanel backVerticalPanel = new VerticalPanel();
		final HorizontalPanel bannerEmail = new HorizontalPanel();
		bannerEmail.add(banner);
		bannerEmail.add(new HTML(CurrentUser.getInstance().getCurrentUser().email));
		backVerticalPanel.add(bannerEmail);
		
	
		
		// Tab Layout 
		//    "Submission" | "Previous Submissions" | "Admin" | "About"
	    final TabLayoutPanel tabPanel = new TabLayoutPanel(2.5, Unit.EM);
	    tabPanel.setSize("650px", "600px");
	    tabPanel.setAnimationDuration(1000);
	    
	    // Submission Tab  - For now, it's a Grid that holds a "title" label, Cameron's SubmissionWidget, and a "Send" button
		final SubmissionTab submissionTab = new SubmissionTab();
	    tabPanel.add(submissionTab, "Submission");
    
	    // Previous Submissions Tab
	    final PreviousSubmissionTab pSubmissionTab = PreviousSubmissionTab.getInstance();
	    tabPanel.add(pSubmissionTab,"Previous Submissions");
	    
	    // Join Groups Tab
	    final AllGroupsTab allGroupsTab = AllGroupsTab.getInstance();
	    tabPanel.add(allGroupsTab, "Join Groups");
	    
	    //About Tab
		final AboutTab aboutTab = AboutTab.getInstance();
		tabPanel.add(aboutTab, "About");
	    
	    // Add placeholders for tabs soon to come
	    tabPanel.add(new HTML("Preferences"), "Preferences");
	    
	    // Add the Tab Layout to the main vertical panel "backVerticalPanel"
	    backVerticalPanel.add(tabPanel);
	    
	    // Finally, add all of our content to the RootPanel!
		RootPanel.get().add(backVerticalPanel);
	}
	
	private void initNonMember(){
		
		Utils.hideLoadingScreen();
		
		// SoTD Banner - gets put on a vertical panel
		final Image banner = new Image( "/images/banner.png");
		final VerticalPanel backVerticalPanel = new VerticalPanel();
		backVerticalPanel.add(banner);
		
		// Tab Layout 
	    final TabLayoutPanel tabPanel = new TabLayoutPanel(2.5, Unit.EM);
	    tabPanel.setSize("650px", "600px");
	    tabPanel.setAnimationDuration(1000);
	    
	    //"How it works" Tab
		final HowItWorksTab howTab = HowItWorksTab.getInstance();
		tabPanel.add(howTab, "How it works");
		
		//"About" tab
		final AboutTab aboutTab = AboutTab.getInstance();
		tabPanel.add(aboutTab, "About");
		
	    // Add the Tab Layout to the main vertical panel "backVerticalPanel" - Should only have an ABOUT tab
	    backVerticalPanel.add(tabPanel);
		
	    // Finally, add all of our content to the RootPanel!
		RootPanel.get().add(backVerticalPanel);		
	}
}
